package com.zoe.sec03oauth2.config;

import com.zoe.sec03oauth2.service.UserDetailServiceImpl;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Zoe
 * @Date: 2021/03/31
 * @Description
 */
@Configuration
@EnableAuthorizationServer
public class MyAuthorizationConfig extends AuthorizationServerConfigurerAdapter {
    @Resource
    PasswordEncoder passwordEncoder;

    @Resource
    UserDetailServiceImpl userDetailService;

    @Resource
    AuthenticationManager authenticationManager;

    @Resource
    TokenStore tokenStore;

    @Resource
    JwtAccessTokenConverter jwtAccessTokenConverter;

    @Resource
    private JwtTokenEnhancer jwtTokenEnhancer;
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        TokenEnhancerChain enhancerChain = new TokenEnhancerChain();
        List<TokenEnhancer> delegates = new ArrayList<>();
        delegates.add(jwtTokenEnhancer);
        delegates.add(jwtAccessTokenConverter);
        enhancerChain.setTokenEnhancers(delegates);
        endpoints.authenticationManager(authenticationManager)
            .userDetailsService(userDetailService)
            //配置存储令牌策略
            .tokenStore(tokenStore)
            .accessTokenConverter(jwtAccessTokenConverter)
            .tokenEnhancer(enhancerChain);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        //允许表单认证
//        security.allowFormAuthenticationForClients();
//        security.checkTokenAccess("permitAll()");
        // 配置秘钥；配置单点登录必备
        security.tokenKeyAccess("isAuthenticated()");
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()// 使用in-memory存储
            .withClient("client")// client_id
            .secret(passwordEncoder.encode("123"))//客户端密钥
            // 访问令牌失效时间  秒
            .accessTokenValiditySeconds(1800)
            // 刷新令牌失效时间
            .refreshTokenValiditySeconds(1800)
            .authorizedGrantTypes("authorization_code", "password", "refresh_token")// 该client允许的授权类型authorization_code,password,refresh_token,implicit,client_credentials
            .scopes("all")// 允许的授权范围
            .autoApprove(true)// 跳过授权页面，自动授权
            // 加上验证回调地址
            // .redirectUris("http://www.baidu.com");
            .redirectUris("http://localhost:8081/login");
    }


}
